var express = require('express');
var router = express.Router();
const merchantLogin    = require('../main/merchant_login');
const jsLogin          = require('../main/js_login');
const Junglescout      = require('../main/junglescout_login');
const validJunlescout  = require('../valid/jungle_scout');
const validMerchant    = require('../valid/merchant_word');
const jsplugin         = require('../valid/js_plugin');
const elves            = require('../valid/elves');
const elvesLogin       = require('../main/elves_login');
const elvesPluginValid = require('../valid/elves_plugin');
const elvesPluginLogin = require('../main/elves_plugin_login');
const heliumLogin      = require('../main/helium_login');
const heliumValid      = require('../valid/helium');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/validJunlesout',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new validJunlescout();
  result.success = await valid.validCookie(cookie);
  res.send(result);
});

router.post('/validMerchant',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new validMerchant();
  result.success = await valid.valid(cookie);
  res.send(result);
});

router.post('/validJsPlugin',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new jsplugin();
  result.success = await valid.validCookie(cookie);
  console.log(`js plugin valid result:[${result.success}]`);
  res.send(result);
});

router.post('/validElves',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new elves();
  result.success = await valid.valid(cookie);
  res.send(result);
});

router.post('/validElvesPlugin',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new elvesPluginValid();
  result.success = await valid.validCookie(cookie);
  res.send(result);
});

router.post('/validHelium',async (req,res,next) => {
  let cookie = req.body.cookie;
  let result = {
    success:false,
    message:''
  };
  if(!cookie){
    result.message = 'Cookie must not be null';
    res.send(result);
    return;
  }
  let valid = new heliumValid();
  result.success = await valid.valid(cookie);
  res.send(result);
});

router.get('/login',async (req,res,next) => {
  let userName = req.query.userName;
  let password = req.query.password;
  let type = req.query.type;

  password = clearSpecial(password);

  let id = req.query.id;
  console.log(`接收到登录请求 \n userName:${userName} \n password:${password} \n id:${id} \n type:${type} ${2 === type}`);

  let result = {
    id:id,
    cookie:'',
    code:200
  };

  let cookies = '';
  let lo;

  if(2 == type){
    //js插件版
    lo = new jsLogin(userName,password);
  }else if(1 == type){
    //js网页版
    lo = new Junglescout(userName,password);
  }else if(3 == type){
    //卖家精灵
    lo = new elvesLogin(userName,password);
  }else if(4 == type){
    //卖家精灵插件
    lo = new elvesPluginLogin(userName,password);
  }else if(5 == type){
    //魔词
    lo = new merchantLogin(userName,password);
  }else if(6 == type){
    //helium10
    lo = new heliumLogin(userName,password);
  }
  cookies = await lo.do().catch((e) => {
    console.log(`执行登录任务失败 type:${type} e:${e}`);
    result.code = -200;
    return e
  });
  result.cookie = cookies;

  if(!cookies){
    result.code = -200;
  }
  res.send(result);
});

function clearSpecial(str){
  if(!str) return ''
  str = str.replace(/\ +/g,"");
  str = str.replace(/[\r\n]/g,"");
  str = str.replace(/20%/g,"");
  return str;
}

module.exports = router;
